<template>
  <div class="addGroup">
    <el-dialog
      :visible="visible"
      :close-on-click-modal="false"
      width="700px"
      :modal-append-to-body="false"
      :show-close="false"
      top="3vh"
    >
      <div slot="title" class="header">
        <div class="left">
          <div class="icon" />
          <div class="title">新增角色 </div>
        </div>
        <div class="close_icon" @click="close" />
      </div>
      <div class="body">
        <div style="padding: 30px 30px 10px 30px;">
          <el-form ref="roleForm" label-width="80px" :model="roleForm" :rules="rules">
            <el-form-item prop="role_name_zh" label="角色名称">
              <el-input
                v-model="roleForm.role_name_zh"
                type="text"
                placeholder="请输入角色名称"
              />
            </el-form-item>
          </el-form>
        </div>
        <div class="role_permission_title">
          <div>角色权限</div>
          <div class="search">
            <el-input v-model="searchKey" size="small" prefix-icon="el-icon-search" placeholder="请输入关键词进行搜索" />
          </div>
        </div>
        <div class="role_permission_list">
          <el-tree
            ref="tree"
            :data="treeList"
            show-checkbox
            check-strictly
            default-expand-all
            node-key="id"
            highlight-current
            :props="defaultProps"
            :filter-node-method="filterNode"
          />
        </div>
      </div>
      <div slot="footer" class="footer">
        <el-button class="btn" plain @click="close">取消</el-button>
        <el-button class="btn" type="primary" :loading="loading" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menu_list } from '@/api/menu'
import { role_add } from '@/api/role'
export default {
  name: 'AccountEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // 这里存放数据
    return {
      searchKey: '',
      loading: false,
      roleForm: {
        rule_ids: [],
        role_name_zh: ''
      },
      rules: {
        role_name_zh: { required: true, message: '请输入角色名称', trigger: 'blur' }
      },
      treeList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
        // disabled: function(data, node) {
        //   return data.ismenu === 1
        // }
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    searchKey(val) {
      this.$refs.tree.filter(val)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  async created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getMenuList()
  },
  // 生命周期 - keep-alive
  async activated() {},
  // 方法集合
  methods: {
    async getMenuList() {
      try {
        this.loading = true
        const { code, data } = await menu_list()
        if (!code) {
          this.treeList = data
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    // 获取树形结构Key值并赋值在 ruleForm.rules
    getCheckedKeys() {
      const selectThreeKeys = this.$refs.tree.getCheckedKeys()
      const parentThreeKeys = this.$refs.tree.getHalfCheckedKeys()
      this.roleForm.rule_ids = [
        ...selectThreeKeys,
        ...parentThreeKeys
      ]
    },
    submit() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          try {
            this.getCheckedKeys()
            const param = this.roleForm
            const { code } = await role_add(param)
            if (!code) {
              this.close()
              this.$parent.loadData()
              this.$messageOnce.success('操作成功！')
            }
          } catch (error) {
            console.error(error)
          } finally {
            this.loading = false
          }
        } else {
          return false
        }
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.title.indexOf(value) !== -1
    },
    close() {
      this.$refs.tree.setCheckedKeys([])
      this.$refs.roleForm.resetFields()
      this.$emit('close')
    }
  },
  // 进入页面前
  beforeRouteEnter(to, from, next) {
    next((vm) => {})
  },
  // 离开页面前
  beforeRouteLeave(to, from, next) {
    next()
  }
}
</script>
<style lang="scss" scoped>
.addGroup {
	.header{
		width: 100%;
		display: flex;
		justify-content: space-between;
    align-items: center;
    background: #FAFAFA;
		.left{
			display: flex;
			.icon{
				background: url('../../assets/dialog/add_icon.png');
				@include dialog_head_icons;
			}
			.title{
				font-size: 14px;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 500;
				color: #000000;
				line-height: 30px;
			}
		}
    .close_icon {
				width:25px;
        height: 24px;
				background: url('../../assets/close-icon.png');
				background-size: 100%;
        cursor: pointer;
		}
	}
  .footer {
    text-align: center;
    .btn{
      width: 80px;
    }
  }
}

.body {
  height: 75vh;
  .role_permission_title {
    display: flex;
    justify-content: space-between;
    padding: 0 30px;
    width: 100%;
    height: 54px;
    background: #FAFAFA;
    font-size: 16px;
    font-family: SourceHanSansCN-Medium, SourceHanSansCN;
    font-weight: 500;
    color: #141414;
    line-height: 54px;
  }
  .role_permission_list {
    padding: 0 150px;
    max-height: 570px;
    overflow: auto;
  }
}

::v-deep .el-dialog__body {
  padding: 0;
  color: #606266;
  font-size: 14px;
  word-break: break-all;
}

// ::v-deep .el-input__inner{
//   width: 500px;
// 	border-radius: 4px !important;
//   padding-right: 45px !important;
// }

::v-deep .el-dialog__footer{
  height: 80px;
  padding: 20px;
  box-shadow: 0px -5px 20px 0px rgba(71, 73, 155, 0.06);
  border-radius: 0px 0px 4px 4px;
}
</style>
